package com.mf.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.Workday;
import com.mf.service.*;
import com.mf.service.CalculateService.Calculate4Service;
import com.mf.service.CalculateService.CalculateService8;
import com.mf.utils.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: lmf
 * @Create: 2024/6/10 22:12
 * Description:  更新历史股票代码
 */
@Component
public class UpdateHistoryCode {
    @Resource
    private ICodeHistoryService codeHistoryService;

    @Resource
    private ICodeService codeService;

    @Resource
    private IWorkdayService workdayService;

    @Resource
    private ICanPurchaseDmService canPurchaseDmService;

    @Resource
    private IPurchaseRecordService purchaseRecordService;

    @Resource
    private CalculateService8 calculateService8;

    /**
     * 每天4点更新当天股票数据
     * 0 0 16 * * ?
     */

//    @Scheduled(cron="5 * * * * ? ")
    // 0 0 4 * * ? *
    @Scheduled(cron = "0 30 16 * * ?")
    public void updateCode(){
        String date = DateUtil.getToDate();
        System.out.println("任务开始" + date);
        // 工作日才进行更新'
        final LambdaQueryWrapper<Workday> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Workday::getDate,date);
        if (workdayService.getOne(lambdaQueryWrapper) == null){
            System.out.println("当天不是工作日");
            return;
        }
        System.out.println("========开始获取代码================");
        purchaseRecordService.remove(null);
        calculateService8.calculate("000001");
        System.out.println("结束==============================================================");

//        // 获取所有股票代码
//        List<Code> codeList = codeService.findAll();
//        final DateScopedBo dateScopedBo = new DateScopedBo();
//        dateScopedBo.setStartDate(date);
//        dateScopedBo.setEndDate(date);
//
//        for (Code code : codeList) {
//            codeHistoryService.setNewHistory(code.getDm());
//        }
////        final CodeDateScopedBo codeDateScopedBo = new CodeDateScopedBo();
////        BeanUtil.copyProperties(dateScopedBo,codeDateScopedBo);
////
////        // 计算新的结果并批量插入数据库
////        List<String> code =  codeHistoryService.calculateHistoryByDate(codeDateScopedBo);
////        List<CanPurchaseDm> canPurchaseDmList = canPurchaseDmService.getSaveBatchCodeList(code);
////        canPurchaseDmService.saveBatch(canPurchaseDmList);
    }

}
